<template>
  <div>

    <el-form ref="searchForm" :inline="true" :model="searchMap" style="margin-top: 20px">
      <el-form-item prop="username">
        <el-input v-model="searchMap.username" placeholder="编号" style="width: 160px"></el-input>
      </el-form-item>
      <el-form-item prop="name">
        <el-input v-model="searchMap.name" placeholder="姓名" style="width: 160px"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="fetchData">查询</el-button>
        <el-button type="primary" @click="handleAdd">新增</el-button>
        <el-button  @click="resetForm('searchForm')">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="list"
      height="380"
      border
      style="width: 100%">
      <!--            type="index"获取索引值，从1开始，label显示标题，prop数据字段名，width是列的宽度-->
      <el-table-column type="index" label="序号" width="60"></el-table-column>
      <el-table-column prop="username" label="编号" width="180"></el-table-column>
      <el-table-column prop="name" label="姓名"></el-table-column>
      <el-table-column prop="age" label="年龄"></el-table-column>
      <el-table-column prop="mobile" label="电话"></el-table-column>
      <el-table-column prop="salary" label="工资"></el-table-column>
      <el-table-column prop="entryDate" label="入职时间"></el-table-column>
      <el-table-column label="操作" width="160">
        <!--                slot-scope="scope"就表示当前行的范围-->
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.row.id)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 30,50]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>

    <el-dialog title="员工新增" :visible.sync="dialogFormVisible" width="500px">
      <el-form status-icon
        label-width="100px" label-position="right" style="width: 400px"
        ref="pojoForm"  :rules="rules" :model="pojo">
        <el-form-item label="编号" prop="username">
          <el-input  v-model="pojo.username"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="pojo.name" ></el-input>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="pojo.age"></el-input>                </el-form-item>
        <el-form-item label="电话" prop="mobile">
          <el-input  v-model="pojo.mobile"></el-input>
        </el-form-item>
        <el-form-item label="工资" prop="salary">
          <el-input  v-model="pojo.salary"></el-input>
        </el-form-item>
        <el-form-item label="入职时间" prop="entryDate">
          <!--                value-format是指定绑定值的格式-->
          <el-date-picker value-format="yyyy-MM-dd" v-model="pojo.entryDate" type="date" placeholder="请点击选择">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="pojo.id===null?addData('pojoForm'):updateData('pojoForm')">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import staffApi from '../../api/staff'
export default {
  name: 'Staff',

  data () {
    return {
      list: [],
      total: 0,
      pageSize: 1,
      currentPage: 1,
      searchMap: {
        id: null,
        username: '',
        name: ''
      },
      pojo: {
        id: null,
        username: '',
        name: '',
        age: '',
        mobile: '',
        salary: '',
        entryDate: ''
      },
      dialogFormVisible: false,
      rules: {
        username: [{ required: true, message: '编号不能为空', trigger: 'blur' }],
        name: [{ required: true, message: '姓名不能为空', trigger: 'blur' }]
      }

    }
  },
  created () {
    this.fetchData()
  },

  methods: {
    fetchData () {
      staffApi.search(this.currentPage, this.pageSize, this.searchMap).then(response => {
        // console.log(response)
        const resp = response.data.data
        this.total = resp.total
        // 这里list表示10个数组
        this.list = resp.rows
      })
    },
    handleEdit (id) {
      // 先打开新增窗口
      this.handleAdd()
      // 查询数据
      staffApi.getById(id).then(response => {
        const resp = response.data
        if (resp.flag) {
          this.pojo = resp.data
        }
      })
    },
    handleDelete (id) {
      this.$confirm('确认删除这条记录么？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 确认
        staffApi.deleteById(id).then(response => {
          const resp = response.data
          // 提示信息
          this.$message({
            type: resp.flag ? 'success' : 'error',
            message: resp.message
          })
          if (resp.flag) {
            // 删除成功，刷新列表
            this.fetchData()
          }
        }).catch(() => {
          // 取消删除，不理会
        })
      })
    },
    handleCurrentChange (val) {
      // console.log(`当前是${val}`)
      this.currentPage = val
      this.fetchData()
    },
    handleSizeChange (val) {
      // console.log(`size是${val}`)
      this.pageSize = val
      this.fetchData()
    },
    handleAdd () {
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs.pojoForm.resetFields()
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    },
    addData (formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          staffApi.add(this.pojo).then(response => {
            const resp = response.data
            if (resp.flag) {
              this.fetchData()
              this.dialogFormVisible = false
              this.$message({
                type: 'success',
                message: resp.message + '，初始密码为123456！'
              })
            } else {
              this.$message({
                type: 'warning',
                message: '新增失败'
              })
            }
          })
        } else {
          return false
        }
      })
    },
    updateData (formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          staffApi.update(this.pojo).then(response => {
            const resp = response.data
            if (resp.flag) {
              this.fetchData()
              this.dialogFormVisible = false
            } else {
              this.$message({
                type: 'warning',
                message: resp.message
              })
            }
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style scoped>
  .el-form{
    margin-top: 20px;
  }

</style>
